#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QString id,int role,QSqlDatabase* db,QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->setCentralWidget(ui->stackedWidget);//设置中心组件

    //初始化用户登陆参数
    m_db = *db;
    m_id = id;
    m_role = role;

    //检测账号是否被封禁
    QSqlQuery query;
    query.exec("select * from tb_user where id="+m_id+";");
    query.first();
    if(query.isValid()){
        if(query.value("state").toInt() == 0){
            QMessageBox::warning(0,"警告","此账号已被停用,请联系管理员.");
            this->deleteLater();
        }
    }

    if(m_role == 1){
        ini();
    }else if(m_role == 0){
        iniUser();
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ini()
{
    ui->stackedWidget->setCurrentWidget(ui->pagUser);

    QSqlQuery query;
    query.exec("select * from tb_admin where id="+m_id+";");
    query.first();
    this->setWindowTitle("游戏角色信息管理系统欢迎你!"+query.value("account").toString());

    m_userModel = new QSqlTableModel(this);
    m_illModel = new QSqlTableModel(this);

    m_userModel->setTable("tb_user");
    m_illModel->setTable("tb_illegal");

    m_userModel->select();
    m_illModel->select();

    m_userModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    m_illModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    m_userModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_userModel->setHeaderData(1,Qt::Horizontal,"账号",Qt::DisplayRole);
    m_userModel->setHeaderData(2,Qt::Horizontal,"密码",Qt::DisplayRole);
    m_userModel->setHeaderData(3,Qt::Horizontal,"名字",Qt::DisplayRole);
    m_userModel->setHeaderData(4,Qt::Horizontal,"状态",Qt::DisplayRole);
    m_userModel->setHeaderData(5,Qt::Horizontal,"手机号码",Qt::DisplayRole);
    m_userModel->setHeaderData(6,Qt::Horizontal,"属性",Qt::DisplayRole);

    m_illModel->setHeaderData(0,Qt::Horizontal,"编号",Qt::DisplayRole);
    m_illModel->setHeaderData(1,Qt::Horizontal,"管理员编号",Qt::DisplayRole);
    m_illModel->setHeaderData(2,Qt::Horizontal,"用户编号",Qt::DisplayRole);
    m_illModel->setHeaderData(3,Qt::Horizontal,"描述",Qt::DisplayRole);
    m_illModel->setHeaderData(4,Qt::Horizontal,"时间",Qt::DisplayRole);

//    ui->tvUser->hideColumn(6);
    ui->tvUser->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvIll->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tvUser->setModel(m_userModel);
    ui->tvIll->setModel(m_illModel);

    ui->btnDeleteRole->hide();

    //    ui->tvUser->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvIll->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void MainWindow::iniUser()
{
    QSqlQuery query;
    query.exec("select * from tb_user where id="+m_id+";");
    query.first();
    this->setWindowTitle("游戏角色信息管理系统欢迎你!"+query.value("account").toString());

    m_userModel = new QSqlTableModel(this);
    m_userModel->setTable("tb_user");
    m_userModel->select();
    m_userModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    int row = -1;
    for(int i = 0;i < m_userModel->rowCount(); ++i){
        if(m_userModel->data(m_userModel->index(i,0)).toString() == m_id){
            row = i;
            break;
        }
    }

    if(row == -1){
        QMessageBox::warning(0,"警告","数据库检索失败!");
        return;
    }

    ui->labUserId->setText(m_userModel->data(m_userModel->index(row,0)).toString());
    ui->labUserAccount->setText(m_userModel->data(m_userModel->index(row,1)).toString());
    ui->editUserPswd->setText(m_userModel->data(m_userModel->index(row,2)).toString());
    ui->editUserName->setText(m_userModel->data(m_userModel->index(row,3)).toString());
    if(m_userModel->data(m_userModel->index(row,4)).toString() == "1"){
        ui->combUserState->setCurrentIndex(1);

    }else if(m_userModel->data(m_userModel->index(row,4)).toString() == "0"){
        ui->combUserState->setCurrentIndex(0);
    }
    ui->editUserPhone->setText(m_userModel->data(m_userModel->index(row,5)).toString());
    ui->stackedWidget->setCurrentWidget(ui->pagUserDetail);

    QString atrStr = m_userModel->data(m_userModel->index(row,6)).toString();
    QStringList theList = atrStr.split("&");

    ui->editRoleName->setText(theList.at(0));
    ui->combRoleSex->setCurrentIndex(theList.at(1).toInt());
    ui->editRolePro->setText(theList.at(2));
    ui->editRoleLv->setText(theList.at(3));
    ui->editRoleGold->setText(theList.at(4));
    ui->editRoleAtk->setText(theList.at(5));
    ui->editRoleDef->setText(theList.at(6));
    ui->editRoleMag->setText(theList.at(7));

    ui->btnUserCancel->hide();
    ui->toolBar->hide();
    ui->combUserState->setEnabled(false);

    ui->stackedWidget->setCurrentWidget(ui->pagUserDetail);
}


void MainWindow::on_tvUser_doubleClicked(const QModelIndex &index)
{
    if(m_userModel->isDirty()){
        QMessageBox::warning(0,"警告","请先保存修改再执行此操作.");
        return;
    }

    int row = index.row();
    ui->labUserId->setText(m_userModel->data(m_userModel->index(row,0)).toString());
    ui->labUserAccount->setText(m_userModel->data(m_userModel->index(row,1)).toString());
    ui->editUserPswd->setText(m_userModel->data(m_userModel->index(row,2)).toString());
    ui->editUserName->setText(m_userModel->data(m_userModel->index(row,3)).toString());
    if(m_userModel->data(m_userModel->index(row,4)).toString() == "1"){
        ui->combUserState->setCurrentIndex(1);

    }else if(m_userModel->data(m_userModel->index(row,4)).toString() == "0"){
        ui->combUserState->setCurrentIndex(0);
    }
    ui->editUserPhone->setText(m_userModel->data(m_userModel->index(row,5)).toString());
    ui->stackedWidget->setCurrentWidget(ui->pagUserDetail);

    QString atrStr = m_userModel->data(m_userModel->index(row,6)).toString();
    QStringList theList = atrStr.split("&");

    ui->editRoleName->setText(theList.at(0));
    ui->combRoleSex->setCurrentIndex(theList.at(1).toInt());
    ui->editRolePro->setText(theList.at(2));
    ui->editRoleLv->setText(theList.at(3));
    ui->editRoleGold->setText(theList.at(4));
    ui->editRoleAtk->setText(theList.at(5));
    ui->editRoleDef->setText(theList.at(6));
    ui->editRoleMag->setText(theList.at(7));
}


void MainWindow::on_btnUserCancel_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagUser);
}


void MainWindow::on_btnUserReset_clicked()
{
    QModelIndex index;
    for(int i = 0;i < m_userModel->rowCount(); ++i){
        if(ui->labUserId->text() == m_userModel->data(m_userModel->index(i,0)).toString()){
            index = m_userModel->index(i,0);
            break;
        }
    }
    int row = index.row();
    ui->labUserId->setText(m_userModel->data(m_userModel->index(row,0)).toString());
    ui->labUserAccount->setText(m_userModel->data(m_userModel->index(row,1)).toString());
    ui->editUserPswd->setText(m_userModel->data(m_userModel->index(row,2)).toString());
    ui->editUserName->setText(m_userModel->data(m_userModel->index(row,3)).toString());
    if(m_userModel->data(m_userModel->index(row,4)).toString() == "1"){
        ui->combUserState->setCurrentIndex(1);

    }else if(m_userModel->data(m_userModel->index(row,4)).toString() == "0"){
        ui->combUserState->setCurrentIndex(0);
    }
    ui->editUserPhone->setText(m_userModel->data(m_userModel->index(row,5)).toString());
    ui->stackedWidget->setCurrentWidget(ui->pagUserDetail);

    QString atrStr = m_userModel->data(m_userModel->index(row,6)).toString();
    QStringList theList = atrStr.split("&");

    ui->editRoleName->setText(theList.at(0));
    ui->combRoleSex->setCurrentIndex(theList.at(1).toInt());
    ui->editRolePro->setText(theList.at(2));
    ui->editRoleLv->setText(theList.at(3));
    ui->editRoleGold->setText(theList.at(4));
    ui->editRoleAtk->setText(theList.at(5));
    ui->editRoleDef->setText(theList.at(6));
    ui->editRoleMag->setText(theList.at(7));
}


void MainWindow::on_btnUserSubmit_clicked()
{
    QModelIndex index;
    for(int i = 0;i < m_userModel->rowCount(); ++i){
        if(ui->labUserId->text() == m_userModel->data(m_userModel->index(i,0)).toString()){
            index = m_userModel->index(i,0);
            break;
        }
    }
    int row = index.row();
    QString atrStr = m_userModel->data(m_userModel->index(row,6)).toString();
    QStringList theList = atrStr.split("&");

    QSqlQuery query;
    QString id = ui->labUserId->text();
    QString name = ui->editUserName->text();
    QString pswd = ui->editUserPswd->text();
    QString state =  QString::asprintf("%d",ui->combUserState->currentIndex()) ;
    QString phone = ui->editUserPhone->text();

    QString theStr;
    theStr += ui->editRoleName->text() + "&";
    theStr += QString::asprintf("%d",ui->combRoleSex->currentIndex()) + "&";
    theStr += ui->editRolePro->text() + "&";
    theStr += ui->editRoleLv->text() + "&";
    theStr += ui->editRoleGold->text() + "&";
    theStr += ui->editRoleAtk->text() + "&";
    theStr += ui->editRoleDef->text() + "&";
    theStr += ui->editRoleMag->text();

    if(m_role == 0){//如果是用户登录
        int result = QMessageBox::question(0,"提示","如果修改敏感信息可能会导致本账号被封禁,请确认您的修改.",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Cancel);
        if(result != QMessageBox::Ok){
            return;
        }
    }

    if(!query.exec("update tb_user set pswd='"+pswd+"',name='"+name+"',state="+state+",phone='"+phone+"',attribute='"+theStr+"' where id="+id+";")){
        QMessageBox::warning(0,"警告","数据修改失败!");
        return;
    }

    //检测是否有违规修改
    if(m_role == 0){
        if(theList.at(3) != ui->editRoleLv->text() || theList.at(4) != ui->editRoleGold->text() || theList.at(5) != ui->editRoleAtk->text() || theList.at(6) != ui->editRoleDef->text() || theList.at(7) != ui->editRoleMag->text()){
            query.exec("insert into tb_illegal(adminid,userid,describ) values(-1,"+m_id+",'违规修改等级,经验,金币,魔力,攻击力或防御值');");



        }

            //
    }
    QMessageBox::information(0,"提示","提交成功!");
    m_userModel->select();

}


void MainWindow::on_btnFind_clicked()
{
    if(ui->editFind->text().isEmpty()){
        ui->tvUser->clearSelection();
        return;
    }

    QString str = ui->editFind->text();
    for(int i = 0;i < m_userModel->rowCount(); ++i){
        for(int j = 0;j < m_userModel->columnCount(); ++j){
            if(m_userModel->data(m_userModel->index(i,j)).toString() == str){
                ui->tvUser->setCurrentIndex(m_userModel->index(i,j));
                break;
            }
        }
    }
}


void MainWindow::on_btnAppend_clicked()
{
    m_userModel->insertRow(m_userModel->rowCount());
}


void MainWindow::on_btnDelete_clicked()
{
    if(ui->tvUser->currentIndex().isValid()){
        m_userModel->removeRow(ui->tvUser->currentIndex().row());
    }
}


void MainWindow::on_btnReset_clicked()
{
    m_userModel->select();
}


void MainWindow::on_btnSubmit_clicked()
{
    if(m_userModel->isDirty()){
        if(m_userModel->submitAll() == true){
            QMessageBox::information(0,"提示","提交成功!");

        }else{
            QMessageBox::warning(0,"警告","提交失败!");
        }
    }
}


void MainWindow::on_btnDeleteRole_clicked()
{
    int result = QMessageBox::question(0,"提示","您确定删除此账号角色?",QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Cancel);
    if(result == QMessageBox::Ok){
        QSqlQuery query;
        query.exec("delete from tb_user where id="+m_id+";");
        this->close();
        logui* l = new logui;
        l->show();
    }
}


void MainWindow::on_actUser_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagUser);
}


void MainWindow::on_actIll_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagIll);
}

